# -*- coding: cp936 -*-
import logging

from sqlalchemy import or_, desc

from pylons import request, response, session, tmpl_context as c, url
from pylons.controllers.util import abort, redirect

from fidoweb.lib.base import Session, BaseController, render
from fidoweb.lib.algorithm import checkTimeScope

log = logging.getLogger(__name__)

#	Written by Hanxu, Modified by Sunzheng
class SearchController(BaseController):
    def index(self) :
		c.errMsg = ''
		c.result = None
		from fidoweb.lib.formChecker import FormChecker
		fc = FormChecker()
		if (not 'page' in request.GET) : c.page = 0
		else : 
			c.page = fc.strToInt(request.GET['page'])
			if (c.page == None) : c.page = 0
		if ('q' in request.GET and 'type' in request.GET and len(request.GET['q']) > 0 and len(request.GET['q']) <= 30) :
			c.searchType = request.GET['type']
			if (not checkTimeScope('SearchController.index', 5)) : c.errMsg = 'too fast'
			else :
				s = request.GET['q']
				words = s.split()
				while (len(words) > 3) : words.pop()
				
				if (c.searchType == '1' and len(words) > 0) :
					from fidoweb.model.map import SchoolMapLoc
					f = SchoolMapLoc.name.like('%' + words[0] + '%')
					for i in range(1, len(words)) :
						f = or_(f, SchoolMapLoc.name.like('%' + words[i] + '%'))
					for i in range(0, len(words)) :
						f = or_(f, SchoolMapLoc.address.like('%' + words[i] + '%'))
					c.resultCount = Session.query(SchoolMapLoc).filter(f).order_by(desc(SchoolMapLoc.ranking)).count()
					c.result = Session.query(SchoolMapLoc).filter(f).order_by(desc(SchoolMapLoc.ranking)).slice(c.page * 20, c.page * 20 + 20).all()

				if (c.searchType == '2' and len(words) > 0) :
					from fidoweb.model.map import SchoolMapLocTopic
					searchStr = ''
					for w in words :
						w = w.replace('+', '')
						w = w.replace('-', '')
						searchStr = searchStr + ' *' + w + '*'
					c.resultCount = Session.query(SchoolMapLocTopic).filter('MATCH (title,content) AGAINST (\'' + searchStr[1 :] + '\' IN BOOLEAN MODE)').order_by(desc(SchoolMapLocTopic.updateTime)).count()
					c.result = Session.query(SchoolMapLocTopic).filter('MATCH (title,content) AGAINST (\'' + searchStr[1 :] + '\' IN BOOLEAN MODE)').order_by(desc(SchoolMapLocTopic.updateTime)).slice(c.page * 20, c.page * 20 + 20).all()
				
				if (c.searchType == '3' and len(words) > 0) :
					from fidoweb.model.map import Discount
					f = Discount.name.like('%' + words[0] + '%')
					for i in range(1, len(words)) :
						f = or_(f, Discount.name.like('%' + words[i] + '%'))
					c.resultCount = Session.query(Discount).filter(f).order_by(desc(Discount.createTime)).count()
					c.result = Session.query(Discount).filter(f).order_by(desc(Discount.createTime)).slice(c.page * 20, c.page * 20 + 20).all()
		else :
			c.searchType = '1'
		
		from fidoweb.controllers.login import LoginController
		from fidoweb.controllers.map import MapController
		lc = LoginController()
		mc = MapController()
		c.loginUser = lc._getLoginUser()
		c.schools = mc._getSchools()
		c.jsFiles = list()
		c.cssFiles = list()
		c.title = 'Fido - Search'
		c.header = render('global/globalheader.mako')
		c.content = render('search/content.mako')
		return render('global/global.mako')

